﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyQQ
{
    public partial class Frm_Register : Form
    {
        //示例数据库操作对象
        DataOperator dataOperator = new DataOperator();
        public Frm_Register()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置下拉框默认值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Register_Load(object sender, EventArgs e)
        {
            CBoxStar.SelectedIndex = CBoxBloodType.SelectedIndex = 0;
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRegister_Click(object sender, EventArgs e)
        {
            //验证昵称
            if (string.IsNullOrEmpty(txtNickName.Text.Trim()) || txtNickName.Text.Length > 20)
            {
                MessageBox.Show("昵称格式输入有误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //为控件设置输入焦点
                txtNickName.Focus();
                return;
            }
            //验证年龄
            if (string.IsNullOrEmpty(TxtAge.Text.Trim()))
            {
                MessageBox.Show("请输入年龄!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtAge.Focus();
                return;
            }
            //验证性别
            if (!RbtnFemale.Checked && !RbtnMable.Checked)
            {
                MessageBox.Show("请选择性别!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LblSex.Focus();
                return;
            }
            //验证密码
            if (string.IsNullOrEmpty(TxtPwd.Text.Trim()))
            {
                MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtPwd.Focus();
                return;
            }
            //验证重新输入密码
            if (string.IsNullOrEmpty(TxtPwdAgain.Text.Trim()))
            {
                MessageBox.Show("请输入确认密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtPwdAgain.Focus();
                return;
            }
            //判断两次密码是否一致
            if (TxtPwd.Text.Trim()!=TxtPwdAgain.Text.Trim())
            {
                MessageBox.Show("两次密码不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtPwdAgain.Focus();
                return;
            }
            //QQ号
            var myQQNumber = 0;
            //弹出的消息
            var message = "";
            //获得选中的性别
            var sex = RbtnMable.Checked ? RbtnMable.Text : RbtnFemale.Text;
            //插入数据
            var sql = string.Format("insert into tb_User (Pwd,NickName,Sex,Age,Name,Star,BloodType) " +
                "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}');select @@Identity from tb_User", TxtPwd.Text.Trim(),
                txtNickName.Text.Trim(), sex, int.Parse(TxtAge.Text.Trim()), CBoxStar.Text, txtName.Text.Trim(), CBoxBloodType.Text);
            SqlCommand command = new SqlCommand(sql, DataOperator.connection);
            DataOperator.connection.Open();
            //执行sql语句,返回受影响的行数
            var result = command.ExecuteNonQuery();
            if (result == 1)
            {
                sql = "select SCOPE_IDENTITY() from tb_User";
                command = new SqlCommand(sql, DataOperator.connection);
                //获取最新增加的账号
                myQQNumber = Convert.ToInt32(command.ExecuteScalar());
                message = string.Format("注册成功，你的QQ号码是:" + myQQNumber);
            }
            else
            {
                message = "注册失败，请重试!";
            }
            DataOperator.connection.Close();
            MessageBox.Show(message, "注册结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
    }
}
